package com.caoxw.Android.CityBus;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class BusInfoActivity extends ActivityEx {
	private TextView	m_objBusName;
	private	ListView	m_objGoStationList;
	private	ListView	m_objBackStationList;
	private	TextView	m_objInfo;
	  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stationinfo);
        m_objBusName = (TextView)this.findViewById(R.id.busName1);
        m_objGoStationList = (ListView)this.findViewById(R.id.stationsList1);
        m_objBackStationList = (ListView)this.findViewById(R.id.stationsList2);
        m_objInfo = (TextView)this.findViewById(R.id.textView3);
        Bundle bundle=getIntent().getExtras();
        String strBusName=bundle.getString("BusName");
        m_objBusName.setText(strBusName);
        
        BusInfo objBus = ((CityBusApp)getApplicationContext()).m_objBusScan.getBusInfo(strBusName.trim());
        if(null != objBus){
	        //Log.d("BusName",objBus.m_BusName);
	        m_objGoStationList.setAdapter(fillListAdapt(objBus.m_GoStations));
	        m_objBackStationList.setAdapter(fillListAdapt(objBus.m_BackStations));
	        m_objInfo.setText(objBus.m_OtherInfo);
        }
        m_objGoStationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    		@SuppressWarnings("unchecked")
    		public void onItemClick(AdapterView<?> parent, View view,
    				int position, long id) {
    			m_objGoStationList.requestFocusFromTouch();
    			m_objGoStationList.setSelection(position);
    			String strStationName = ((HashMap<String, String>) (m_objGoStationList.getAdapter().getItem(position))).get("ItemTitle").toString().trim();
    			Intent intent = new Intent(getApplicationContext(),BusInfoActivity.class);
    			Bundle bundle = new Bundle();
    			bundle.putString("StationName", strStationName);
    			intent.putExtras(bundle);
    			setResult(RESULT_OK, intent);
    			finish();
    		}
    	});
        m_objBackStationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    		@SuppressWarnings("unchecked")
    		public void onItemClick(AdapterView<?> parent, View view,
    				int position, long id) {
    			m_objBackStationList.requestFocusFromTouch();
    			m_objBackStationList.setSelection(position);
    			String strStationName = ((HashMap<String, String>) (m_objBackStationList.getAdapter().getItem(position))).get("ItemTitle").toString().trim();
    			Intent intent = new Intent(getApplicationContext(),BusInfoActivity.class);
    			Bundle bundle = new Bundle();
    			bundle.putString("StationName", strStationName);
    			intent.putExtras(bundle);
    			setResult(RESULT_OK, intent);
    			finish();
    		}
    	});
    }
    
    private SimpleAdapter fillListAdapt(ArrayList<String> listData){
	  	ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();  
	  	for(int i=0;i<listData.size();i++)  
	  	{  
	  		HashMap<String, String> map = new HashMap<String, String>();  
	  		//map.put("ItemImage", R.drawable.checked);//图像资源的ID  
	  		map.put("ItemTitle", listData.get(i));  
	  		//map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");  
	  		listItem.add(map);  
	  	}  
	  	SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源
	  			  	R.layout.list_item_1,//ListItem的XML实现 
	  			  	//动态数组与ImageItem对应的子项
	  			  	new String[] {"ItemTitle"},
	  			  	//ImageItem的XML文件里面的一个ImageView,两个TextView ID 
	  			  	new int[] {R.id.ItemTitle}
	  	  );  
	  	return listItemAdapter;
    }
    
    public void onFlingLeft() {
		// TODO Auto-generated method stub
		this.finish();
 
	}

}
